package com.sino.operation.Exception.exceptionRoot;


import com.sino.operation.commons.commonEnum.ClassNameEnum;
import com.sino.operation.commons.response.CenterResponseEntity;
import com.sino.operation.commons.response.CenterResponseEnum;
import com.sino.operation.peripheral.esc.response.EscResponseDto;
import com.sino.operation.util.EscResTest;
import com.sino.operation.util.ResponseFieldUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.DataBinder;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TOD
 *
 * @author Administrator
 * date 2021/8/12 0012 23:21
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {

    @ExceptionHandler(Exception.class)
    public EscResponseDto exceptionHandler(Exception e){
        log.error("错误信息",e);

        e.printStackTrace();
        EscResponseDto escReponse = EscResTest.getEscReponse();
        CenterResponseEntity<String> entity = CenterResponseEntity.buildError(String.class).setData(e.getMessage());
        escReponse.getEscResBody().setCenterResponseEntity(entity);
        return escReponse;
    }

    @ExceptionHandler(SinoException.class)
    @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
    public EscResponseDto SinoExceptionHandler(SinoException e){
        log.info("错误信息",e);
        System.out.println(e.getCode());
        System.out.println(e.getMessage());
        e.printStackTrace();
        EscResponseDto escReponse = EscResTest.getEscReponse();
        CenterResponseEntity<Object> entity = new CenterResponseEntity<>(e.getCode(), e.getMessage(), null);
        escReponse.getEscResBody().setCenterResponseEntity(entity);
        return escReponse;
    }



    public HashMap<String,String> jointMsg(List<FieldError> fieldErrors){
        HashMap<String, String> map = new HashMap<>();
        for (FieldError fieldError : fieldErrors) {
            String field = fieldError.getField();
            field=field.substring(field.lastIndexOf(".")+1);
            field= ResponseFieldUtil.changeToStandardByValid(field);
            map.put(field,fieldError.getDefaultMessage());
        }
        return map;
    }

    // 将Spring DataBinder配置为使用直接字段访问
    @InitBinder
    private void activateDirectFieldAccess(DataBinder dataBinder) {
        dataBinder.initDirectFieldAccess();
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        // 获取错误信息集合
        Object target = e.getBindingResult().getTarget();
        Class<?> aClass = target.getClass();
        String name = aClass.getName();
        name=name.substring(name.lastIndexOf(".")+1);
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        // 调用封装好的异常信息拼接方法
        HashMap<String, String> map = jointMsg(fieldErrors);
        log.error(e.getMessage());
        CenterResponseEntity<HashMap> resMap = CenterResponseEntity.buildError(HashMap.class);
        resMap.setCode(CenterResponseEnum.PARAM_ERROR.getCode()).setData(map).setMsg(CenterResponseEnum.PARAM_ERROR.getMessage());
        if (ClassNameEnum.OHTER_CLASS_NAME_BY_COUNTER.getParamName().equals(name)||ClassNameEnum.CURRENT_CLASS_NAME.getParamName().equals(name)){
            EscResponseDto escReponse = EscResTest.getEscReponse();
            escReponse.getEscResBody().setCenterResponseEntity(resMap);
            return escReponse;
        }else {
            return resMap;
        }

    }




}
